package com.PomeloMusic.activity;

import android.app.AlertDialog;
import android.content.Context;
import android.content.Intent;
import android.content.res.Resources;
import android.net.ConnectivityManager;
import android.net.NetworkInfo.State;
import android.net.wifi.WifiManager;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;

import com.PomeloMusic.service.PlayerService;
import com.PomeloMusic.util.ProgressWheel;

public class WelcomeActivity extends MMAbstractActivity {
	/** Called when the activity is first created. */

	private ProgressWheel waitProgressBar;

	public WelcomeActivity() {
		super();
		// controller = new MainController(this);
	}

	private Handler wifiHandler = new Handler() {

		@Override
		public void handleMessage(Message msg) {
			// TODO Auto-generated method stub
			Resources res = WelcomeActivity.this.getResources();
			Intent serviceIntent = new Intent(getApplicationContext(),
					PlayerService.class);
			getApplicationContext().startService(serviceIntent);
			switch (msg.what) {
			case 1: {
				wifiAlert();
			}
				break;
			case 2: {
				new Thread(new Runnable() {
					@Override
					public void run() {
						try {
							Thread.sleep(4500);
						} catch (InterruptedException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						}
						Intent intent = new Intent(WelcomeActivity.this,
								PlayerActivity.class);
						WelcomeActivity.this.startActivity(intent);
						WelcomeActivity.this.finish();
					}
				}).start();

			}
				break;
			}
			super.handleMessage(msg);
		}

	};

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		setContentView(R.layout.welcome);
		this.waitProgressBar = (ProgressWheel) findViewById(R.id.waitProgressBar);
		waitProgressBar.spin();
		new Thread(new Runnable() {

			@Override
			public void run() {
				// TODO Auto-generated method stub
				WifiManager wifiManager = (WifiManager) getSystemService(Context.WIFI_SERVICE);
				if (!wifiManager.isWifiEnabled()
						|| !isWiFiAvaliable(WelcomeActivity.this)) {
					wifiHandler.sendEmptyMessage(1);
				} else {
					wifiHandler.sendEmptyMessage(2);
				}
				// WelcomeActivity.this.handler.sendEmptyMessage(0);

			}

		}).start();
	}


	private boolean isWiFiAvaliable(Context inContext) {
		Context context = inContext.getApplicationContext();
		ConnectivityManager connectivity = (ConnectivityManager) context
				.getSystemService(Context.CONNECTIVITY_SERVICE);
		State state = connectivity
				.getNetworkInfo(ConnectivityManager.TYPE_WIFI).getState();
		return state == State.CONNECTED;
	}

	@Override
	protected void onDestroy() {
		// TODO Auto-generated method stub
		getWindow().setFlags(~WindowManager.LayoutParams.FLAG_FULLSCREEN,
				WindowManager.LayoutParams.FLAG_FULLSCREEN);
		super.onDestroy();
	}

	@Override
	protected void setListeners() {
		// TODO Auto-generated method stub

	}

	private void wifiAlert() {

		final AlertDialog alertDialog = new AlertDialog.Builder(
				WelcomeActivity.this).create();
		alertDialog.show();
		Window window = alertDialog.getWindow();
		// 设置dialog位置
		WindowManager.LayoutParams layoutParams = alertDialog.getWindow()
				.getAttributes();
		layoutParams.gravity = (Gravity.CENTER);
		alertDialog.getWindow().setAttributes(layoutParams);

		window.setContentView(R.layout.dialog_wifi);
		window.setBackgroundDrawableResource(android.R.color.transparent);
		Button sure = (Button) window.findViewById(R.id.btn_dialog_left);
		Button cancel = (Button) window.findViewById(R.id.btn_dialog_right);

		sure.setOnClickListener(new OnClickListener() {

			public void onClick(View v) {
				// TODO Auto-generated method stub
				alertDialog.cancel();
				WelcomeActivity.this.finish();
				Intent intent = new Intent(getApplicationContext(),
						PlayerService.class);
				getApplicationContext().stopService(intent);
			}
		});

		cancel.setOnClickListener(new OnClickListener() {

			public void onClick(View v) {
				// TODO Auto-generated method stub
				alertDialog.cancel();
				Intent intent = new Intent(WelcomeActivity.this,
						PlayerActivity.class);
				WelcomeActivity.this.startActivity(intent);
				WelcomeActivity.this.finish();

			}
		});
	}

}